package 补充数组;

public class 数组复制与合并 {
    public static void main(String[] args) {
        int[] a = new int[5];
        a[0] = (int) (Math.random() * 100);
        a[1] = (int) (Math.random() * 100);
        a[2] = (int) (Math.random() * 100);
        a[3] = (int) (Math.random() * 100);
        a[4] = (int) (Math.random() * 100);
        System.out.println("a数组为：");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "\t");
        }
        System.out.println();
        int[] b = new int[5];
        b[0] = (int) (Math.random() * 100);
        b[1] = (int) (Math.random() * 100);
        b[2] = (int) (Math.random() * 100);
        b[3] = (int) (Math.random() * 100);
        b[4] = (int) (Math.random() * 100);
        System.out.println("b数组为：");
        for (int j = 0; j < b.length; j++) {
            System.out.print(b[j] + "\t");
        }

        System.out.println('\n' + "*********************");
//利用数组arraycopy复制
        int[] c = new int[10];
        // System.arraycopy(src, srcPos, dest, destPos, length)
        // src: 源数组
        // srcPos: 从源数组复制数据的起始位置
        // dest: 目标数组
        // destPos: 复制到目标数组的起始位置
        // length: 复制的长度
        System.arraycopy(a, 0, c, 0, 5);
        System.arraycopy(b, 0, c, 5, 5);
        System.out.println("arraycopy合并数组为：");
        for (int j = 0; j < c.length; j++) {
            System.out.print(c[j] + "\t");
        }
        System.out.println();
        int[] d = new int[10];
        int cl = 0;
        for (int i = 0; i < a.length; i++) {
            d[cl] = a[i];
            cl++;
        }
        cl = 5;
        for (int i = 0; i < b.length; i++) {
            d[cl] = b[i];
            cl++;
        }

        System.out.println("for循环合并数组为：");
        for (int j = 0; j < d.length; j++) {
            System.out.print(d[j] + "\t");
        }


    }

}
